// Town 0, Battle for Gale

begintownscript;

variables;
short i,dead,t,party_level;

body;

beginstate INIT_STATE;
	char_give_item(0,451);
	char_give_item(1,451);
	char_give_item(2,451);
	char_give_item(3,451);
	set_character_facing(1000,6);
	force_view_center(char_loc_x(first_group_member(0)),char_loc_y(first_group_member(0)));
	force_instant_terrain_redraw();
	play_sound(-22);
	reset_dialog();
	add_dialog_str(0,"The sun glares brightly on the grassy field. Birds can be heard chirping in the trees surrounding the glade. Above it all, though, is the sound of clanking metal as dozens of golems march up the hill.",0);
	add_dialog_str(1,"Too, the sound of crows cawing can be heard in the sky above, as they aniticipate the carnage soon to come. Today, the Empire makes it's final stand for Gale against the plague of golems that swarms eastern Valorim.",0);
	add_dialog_str(2,"You join the soldiers of the Empire as they line up to face the golems on the foothills surrounding Gale. The soldiers are uneasy, their moral is wavering. These foes are unlike anything they've seen before.",0);
	add_dialog_str(3,"Already, these monstrosities have ravaged two towns, destroying crops and men on their path to the main city that you now stand to protect. Now only you and this army stand between them and the total destruction of Gale.",0);
	add_dialog_str(4,"The mayor of Gale, in his arrogance, ordered the Dervish in command of the defences of the city, to go and meet the army of golems head on, to wipe them out before they reached the walls of Gale.",0);
	add_dialog_str(5,"Now though, that plan is revealed in all its stupidity. You may very well die here, along with all the other soldiers of the Empire. Yet, you won't give up.",0);
	run_dialog(0);
	while (i < 86) {
		if (creature_type(i) > 214) {
			set_attitude(i,3);
		}
		i = i + 1;
	}
	message_dialog("You may now position your troops. To command a unit, talk to it or, when in combat, use the banner item. Once you are done with that, continue down the hill to start the battle.","");
	
//	set_crime_tolerance(1);
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
	if (get_flag(0,11) == 1 && is_combat() == 0) {
		set_flag(0,11,2);
		reset_dialog();
		add_dialog_str(0,"You look around you. The fighting has stopped and it would seem that all the golems have been destroyed. The soldiers wander around the battlefield, prodding the fallen golem pieces to ensure that none have been left alive.",0);
		add_dialog_str(1,"Finally, after the inspection of the carnage is done to everyone's satisfaction, the soldiers begin to laugh and celebrate, some just standing dumbstruck by your unlikely victory.",0);
		add_dialog_str(2,"Seemingly against all odds, you have managed to defeat the golems, ensuring the safety of the area of Gale for a while. Undoubtedly, there will be more golems to replace these, but for now the city is safe.",0);
		add_dialog_str(3,"Eventually the Empire soldiers begin packing up and returning home in their regiments, singing songs as they go. Many of them thank you profusely and give you tokens of their gratitude, even if it is just a simple handshake.",0);
		if (char_ok(38) > 0 && get_attitude(38) < 10) {
			add_dialog_str(4,"As the troops file out, the Dervish comes up to you. _Take my sword as reward for your help,_ he says, _Without you we would surely have lost._ He hands you a gleaming longsword and then departs with his soldiers.",0);
			reward_give(445);
		}
		add_dialog_str(5,"By the time the last of the soldiers depart, you are loaded down with gold and trinkets of the men's gratitude. Now it is time for you to get under way. (Leave the scenario by going to any edge of the map.)",0);
		i = 6;
		while (i < 86) {
			erase_char(i);
			i = i + 1;
		}
		change_coins(1000);
		i = 0;
		while (i <= 3) {
			if (char_ok(i))
				party_level = party_level + get_level(i);
			i = i + 1;
		}
		party_level = party_level / party_size();
		award_party_xp(1000,party_level);
		run_dialog(0);
	}
	if (get_flag(0,11) == 0) {
		i = 47;
		dead = 0;
		while (i < 86) {
			if (char_ok(i) == 0) { // Check to see if this golem is dead
				dead = dead + 1;
				if (get_ran(1,0,100) < 20 && t < 20 && creature_type(i) != 220) { // 20% chance of golem respawning after it dies unless golem reinforcements are exhausted or creature is a demon golem
					spawn_creature(i);
					print_str_color("Another golem joins the fight!",3);
					dead = dead - 1;
					t = t + 1; // One less golem reinforcement
				}
			}
			i = i + 1;
		}
		if (dead >= 38) { // Check to see if all golems are dead
			set_flag(0,11,1);
		}
	}
	if (char_loc_x(first_group_member(0)) >= 27 && get_flag(0,4) == 0) {
		message_dialog("Above the din of the golems and the cricling carrion, you hear the Dervish signal the charge. The battle has begun.","");
		play_sound(-23);
		i = 0;
		while (i < 86) {
			if (creature_type(i) > 214) {
				set_attitude(i,10);
			}
			set_character_pose(i,2);
			i = i + 1;
		}
		i = 0;
		set_flag(0,4,1); // Battle has started
		force_instant_terrain_redraw();
	}
break;

beginstate 10;
	if (get_flag(0,4) == 0) {
		reset_dialog();
		add_dialog_str(0,"Are you finished positioning the troops?",0);
		add_dialog_choice(0,"Yes.");
		add_dialog_choice(1,"No.");
		if (run_dialog(0) == 1) {
			message_dialog("Above the din of the golems and the cricling carrion, you hear the Dervish signal the charge. The battle has begun.","");
			play_sound(-23);
			i = 0;
			while (i < 86) {
				if (creature_type(i) > 214) {
					set_attitude(i,10);
				}
				set_character_pose(i,2);
				i = i + 1;
			}
			i = 0;
			set_flag(0,4,1); // Battle has started
			force_instant_terrain_redraw();
		}
		else block_entry(1);
	}
break;

beginstate 11;
	block_entry(1);
	reset_dialog();
	add_dialog_str(0,"This way leads back to Gale. Do you leave?",0);
	add_dialog_choice(0,"No, I want to stay.");
	add_dialog_choice(1,"Yes. (Leave scenario)");
	if (run_dialog(1) == 2) {
		if (get_flag(0,11) == 0) {
			message_dialog("You flee the battlefield, watching as the golems march forward to raze the city of Gale. The battle here is lost.","");
			end_scenario(0);
		}
		message_dialog("You leave the victorous battlefield, secure in the knowledge that for now, the advance of the golems is stopped. You've won a great victory here today, but many more battles still lie ahead.","");
		end_scenario(1);
	}
break;